package org.alan7yg.aop_sample;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;

import static org.alan7yg.aop_sample.RespCode.SUCCESS;
import static org.alan7yg.aop_sample.RespCode.SYSTEM_ERR;

/**
 * Created by alan on 8/23/16.
 */
@Aspect
@Slf4j
public class RestControllerAspect {

    @Around("@annotation(org.alan7yg.aop_sample.RestHandle)")
    public CommonHttpResponse<Object> advice(ProceedingJoinPoint pjp) {
        log.debug("Processing restHandle...");
        try {
            return new CommonHttpResponse<>(SUCCESS, pjp.proceed());
        } catch (ServiceException e) {
            log.info("Caught org.alan7yg.aop_sample.ServiceException: {}", e);
            return new CommonHttpResponse<>(e.errCode, null);
        } catch (Throwable throwable) {
            log.warn("Caught Exception in RestControllerAspect.restHandle !", throwable);
            return new CommonHttpResponse<>(SYSTEM_ERR, null);
        }
    }
}
